package com.lingx.web.api.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.lingx.service.LingxService;
import com.lingx.web.api.IApi;
@Component
public class Api9033 extends AbstractDevApi implements IApi{
	@Resource
	private LingxService lingxService;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Override
	public int getApiCode() {
		return 9033;
	}
	@Override
	public String getApiName() {
		return "系统设置";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String method=this.getParamString(params, "method","");
		if("save".equals(method)){
			if(!"true".equals(this.lingxService.getConfigValue("lingx.system.isedit", "true")))  {
				ret.put("message", "操作失败，系统禁止操作!");
				return ret;
			}
			if(this.checkIsNull(params, ret, "key"))return ret;
			String key=this.getParamString(params, "key","");
			String value=this.getParamString(params, "value","");
			if("systemName".equals(key)) {
				this.jdbcTemplate.update("update tlingx_app set name=?",value);
			}else if("systemLogo".equals(key)){
				this.jdbcTemplate.update("update tlingx_app set logo=?",value);
			}else {
				this.lingxService.setConfigValue(key, value);
			}
			
		}else {
			List<Map<String,Object>> list=this.jdbcTemplate.queryForList("select * from tlingx_config_item order by orderindex asc");
			Map<String,Object> data=new HashMap<>();
			for(Map<String,Object> map:list) {
				String key=map.get("key1").toString();
				String type=map.get("type").toString();
				if("systemName".equals(key)) {
					data.put(key,this.jdbcTemplate.queryForObject("select name from tlingx_app limit 1",String.class));
				}else if("systemLogo".equals(key)){
					data.put(key,this.jdbcTemplate.queryForObject("select logo from tlingx_app limit 1",String.class));
				}else {
					switch(type) {
					case "text":
						data.put(key, this.lingxService.getConfigValue(key, ""));
						break;
					case "textarea":
						data.put(key, this.lingxService.getConfigValue(key, ""));
						break;
					case "switch":
						data.put(key, "true".equals(this.lingxService.getConfigValue(key, "")));
						break;
					}
					
				}
				
			}
			ret.put("list", list);
			ret.put("data", data);
			
		}
		
		return ret;
	}

	public boolean isLog() {
		return false;
	}
}
